import com.Async.Callback;
import com.Async.RpcFuture;
import com.client.ClientTerminal;
import com.registration.Register;
import com.serializetool.Request;
import com.serializetool.SerializingUtil;
import com.serializetool.Validation;
import org.junit.Test;

import java.io.IOException;

public class RemoteCall {

    @Test
    public void call() throws IOException, InterruptedException {
//          new client().connect();
//        ChannelFuture client = new ClientBuilder(new ClientTerminal()).connect(new InetSocketAddress("127.0.0.1",6666),new ProxyConnection().proxy(ip,port,builder));
//        Request request = buildErrorRequest("hello");
//        client.channel().writeAndFlush(request);
//        client.channel().closeFuture().sync();
//        String str = "hello";
//        Request request = initRequest(str);
//        ByteBuf buf = channel.alloc().buffer(1024);
//        buf.writeBytes("123\r\n".getBytes("UTF-8"));
//        System.out.println("已传输一条");
//        channel.writeAndFlush(request);
    }

    public Request buildErrorRequest(String str){
        Request request = new Request();
        request.setClassName("com.service.ServiceImpl");
        request.setMethodName("printString");
        request.setParameterTypes(new String[]{"string","java"});
        request.setParameters(new Object[]{str});
        return request;
    }

    public Request buildTrueRequest(String str){
        Request request = new Request();
        request.setMagicNum(Validation.RES_MES.getCode());
        request.setClassName("com.service.ServiceImpl");
        request.setMethodName("printString");
        request.setParameterTypes(new String[]{"string"});
        request.setParameters(new Object[]{str});
        return request;
    }

    @Test
    public void testSerialize(){
        Request request = new Request();
        request.setParameterTypes(new String[]{"com.lang.String","com.lang.Integer"});
        request.setParameters(new Object[]{"1",1});
        byte[] bytes = SerializingUtil.serialize(request);
        System.out.println(SerializingUtil.deserialize(bytes, Request.class));
    }

//
//    @Test
//    public void testSync() throws InterruptedException {
//        String ip = "127.0.0.1";
//        int port = 6666;
//        ClientTerminal client = new ClientTerminal();
//        System.out.println("我要尝试远程调用了");
//        System.out.println("这是同步请求");
//        Response result1 = (Response) client.remoteCall(ip,port,buildTrueRequest("hello"),false);
//        System.out.println("同步请求完了，结果为"+result1.getLastResult());
//        System.out.println("这是异步请求");
//        client.remoteCall(ip,port,buildErrorRequest("你好"),true);
//        System.out.println("异步将自己回调提示");
//        Response result2 = null;
//        while(result2==null){
//            result2 = ResponseListener.getResponse("2");
//        }
//        System.out.println("同步请求完了，结果为"+result2.getLastResult());
//    }

//    @Test
//    public void testASync() throws InterruptedException {
//
////        //创建java 线程池
////        ExecutorService jPool =
////                Executors.newFixedThreadPool(10);
////
////        //包装java线程池，构造guava 线程池
////        ListeningExecutorService gPool =
////                MoreExecutors.listeningDecorator(jPool);
////
////        Callable<Response> callable = new RemoteCallable();
////        //提交烧水的业务逻辑，取到异步任务
////        ListenableFuture<Response> future = gPool.submit(callable);
////        //绑定任务执行完成后的回调，到异步任务
////        Futures.addCallback(callable, new FutureCallback<Boolean>() {
////            public void onSuccess( ) {
////            }
////
////            public void onFailure(Throwable t) {
////                System.out.println("烧水失败，没有茶喝了");
////            }
////        });
//        ClientTerminal client = new ClientTerminal();
//        System.out.println("我要尝试远程调用了");
//        System.out.println("这是异步请求");
//        RpcFuture future = (RpcFuture) client.remoteCall( buildTrueRequest("hello"), true, new Callback() {
//            @Override
//            public void onSuccess(Object param) {
//                System.out.println("成功了:"+param);
//            }
//
//            @Override
//            public void onFailure(Throwable error) {
//                System.out.println("失败了"+error.getStackTrace());
//            }
//        });

//        System.out.println("这是异步请求");
//        client.remoteCall(ip,port,buildErrorRequest("你好"),true);
//        System.out.println("异步将自己回调提示");
//        Response result2 = null;
//        while(result2==null){
//            result2 = ResponseListener.getResponse("2");
//        }
//        System.out.println("同步请求完了，结果为"+result2.getLastResult());
//    }

    @Test
    public void checkValueTest() throws Exception {
        Register register = new Register();
        String zkPath = "/a";
        System.out.println(register.retrieveVal(zkPath));
        register.close();
    }

    @Test
    public void watchTest() throws Exception{
        Register register = new Register();
        register.getWatcher("/com/service");
    }

    @Test
    public void test(){
        String s = "abc";
        System.out.println(s.lastIndexOf("b") + 1);
    }

    @Test
    public void testGetServiceAndASync() throws Exception {
        ClientTerminal client = ClientTerminal.getClientTerminal();
        System.out.println("我要尝试远程调用了");
        System.out.println("这是异步请求");
        String serviceName = "/com/service/print";
        RpcFuture future = (RpcFuture) client.remoteCall(buildTrueRequest("hello"), true, new Callback() {
            @Override
            public void onSuccess(Object param) {
                System.out.println("成功了:"+param);
            }

            @Override
            public void onFailure(Throwable error) {
                System.out.println("失败了"+error.getStackTrace());
            }
        }
        ,serviceName);
        System.out.println(future.get());
//        System.out.println("这是异步请求");
//        client.remoteCall(ip,port,buildErrorRequest("你好"),true);
//        System.out.println("异步将自己回调提示");
//        Response result2 = null;
//        while(result2==null){
//            result2 = ResponseListener.getResponse("2");
//        }
//        System.out.println("同步请求完了，结果为"+result2.getLastResult());
        Thread.sleep(Integer.MAX_VALUE);
    }

}
